package com.itfreer.data.general.domain;

import com.itfreer.data.general.GlobalDataType;
import com.itfreer.data.general.RowBase;
import com.itfreer.utils.StringUtils;
import com.itfreer.utils.TreeNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 定义内存字典操作类
 */
public class MemDomain implements IDomain {

	private HashMap<String, String> domains;
	private String domainAliasName;

	public MemDomain(HashMap<String, String> domains, String domainAliasName) {
		this.domains = domains;
		this.domainAliasName = domainAliasName;
	}

	/**
	 * 获取字典域别名，用于显示
	 * 
	 * @return
	 */
	@Override
	public String getDomainAliasName() {
		return domainAliasName;
	}

	@Override
	public HashMap<String, String> getDomains() {
		return domains;
	}

	private HashMap<String, String[]> codeValuesEx;

	/**
	 * 获取代码值，此值主要用于代码翻译，包括单位的翻译
	 */
	@Override
	public HashMap<String, String[]> getDomainsEx() {
		if (codeValuesEx != null) {
			return codeValuesEx;
		}

		if (StringUtils.IsNullOrSpace(this.getUnitFieldName())) {
			HashMap<String, String> codeValues = getDomains();
			Set<String> keys = codeValues.keySet();
			codeValuesEx = new HashMap<String, String[]>();
			for (String key : keys) {
				String[] value = new String[2];
				value[0] = codeValues.get(key);
				codeValuesEx.put(key, value);
			}
			return codeValuesEx;
		} else {
			codeValuesEx = new HashMap<String, String[]>();
			List<RowBase> codeValues = getDatas();
			for (RowBase row : codeValues) {
				String code = (String) row.getValue(GlobalDataType.String,
						this.getCodeFieldName());
				String caption = (String) row.getValue(GlobalDataType.String,
						this.getCaptionFieldName());
				String unit = (String) row.getValue(GlobalDataType.String,
						this.getUnitFieldName());

				String[] value = new String[2];
				value[0] = caption;
				value[1] = unit;
				codeValuesEx.put(code, value);
			}
			return codeValuesEx;
		}
	}

	private List<RowBase> list;

	@Override
	public List<RowBase> getDatas() {
		if (domains == null) {
			return null;
		}
		if (list != null) {
			List<RowBase> result = new ArrayList<RowBase>();
			result.addAll(list);
			return result;
		}

		list = new ArrayList<RowBase>();
		for (Object item : domains.keySet()) {
			RowBase row = new RowBase();
			row.setValue(this.getCodeFieldName(), item);
			row.setValue(this.getCaptionFieldName(), this.domains.get(item));
			list.add(row);
		}

		List<RowBase> result = new ArrayList<RowBase>();
		result.addAll(list);
		return result;
	}

	private List<Map<String, Object>> listEx2 = null;

	/**
	 * 获取所有数据，并转换成可绑定到TreeGrid的数据对象
	 * 
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getDatasEx2(boolean convertStaticField) {
		if (listEx2 != null) {
			List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
			result.addAll(listEx2);
			return result;
		}

		listEx2 = new ArrayList<Map<String, Object>>();
		Set<String> keys = domains.keySet();
		for (String item : keys) {
			Map<String, Object> row = new HashMap<String, Object>();
			if (convertStaticField) {
				row.put(IDomain.ID, item);
				row.put(IDomain.DOMAINCODE, item);
				row.put(IDomain.DOMAINCAPTION, this.domains.get(item));
			} else {
				row.put(this.getIDFieldName(), item);
				row.put(this.getCodeFieldName(), item);
				row.put(this.getCaptionFieldName(), this.domains.get(item));
			}
			listEx2.add(row);
		}

		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		result.addAll(listEx2);
		return result;
	}

	private List<TreeNode> listEx;

	/**
	 * 获取所有数据，并转换成可绑定的数据对象
	 * 
	 * @return
	 */
	public List<TreeNode> getDatasEx() {
		if (domains == null) {
			return null;
		}

		listEx = new ArrayList<TreeNode>();
		Set<String> keys = domains.keySet();
		for (String item : keys) {
            TreeNode row = new TreeNode();
			row.setid(item);
			row.setText(domains.get(item));
			row.setState("open");
			listEx.add(row);
		}

		List<TreeNode> result = new ArrayList<TreeNode>();
		result.addAll(listEx);
		return result;
	}

	/**
	 * 带政区ID的字典过滤
	 */
	@Override
	public List<TreeNode> getDatasEx(HashMap<String, Object> filterMap,
			String extKey, String extVal) {
		return null;
	}

	@Override
	public List<TreeNode> getDatasEx(HashMap<String, Object> filterMap) {
		if (domains == null) {
			return null;
		}
		List<TreeNode> result = new ArrayList<TreeNode>();
		Set<String> keys = domains.keySet();
		for (String item : keys) {
            TreeNode row = new TreeNode();
			row.setid(item);
			row.setText(domains.get(item));
			row.setState("open");
			result.add(row);
		}

		return filterResult(result, filterMap);
	}

	/**
	 * 按过滤条件过滤数据
	 * 
	 * @param result
	 * @param filterMap
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	private List<TreeNode> filterResult(List<TreeNode> result,
			HashMap<String, Object> filterMap) {
		List<TreeNode> filterResult = new ArrayList<TreeNode>();
		for (int i = 0; i < result.size(); i++) {
            TreeNode node = result.get(i);
			RowBase row = (RowBase) node.getAttributes();
			Set keyArray = filterMap.keySet();
			Iterator iterator = keyArray.iterator();
			while (iterator.hasNext()) {
				String key = iterator.next().toString();
				String rowValue = (String) row.getValue(key);
				String value = (String) filterMap.get(key);
				if (!StringUtils.IsNullOrSpace(rowValue)
						&& rowValue.equals(value)) {
					filterResult.add(node);
				}
			}
		}
		return filterResult;
	}

	/**
	 * 获取所有的记录数 主要用于数据绑定
	 */
	public List<RowBase> getNextDatas(String id) {
		return null;
	}

	/**
	 * 获取下级数据对象，并转换成可绑定对象
	 * 
	 * @param id
	 * @return
	 */
	public List<TreeNode> getNextDatasEx(String id) {
		return null;
	}

	@Override
	public String getIDFieldName() {
		return null;
	}

	@Override
	public String getPIDFieldName() {
		return null;
	}

	@Override
	public String getCodeFieldName() {
		return "CODE";
	}

	@Override
	public String getCaptionFieldName() {
		return "CAPTION";
	}

	@Override
	public String getOrderFieldName() {
		return null;
	}

	@Override
	public String getIsUsedFieldName() {
		return null;
	}

	@Override
	public String getIsEditFieldName() {
		return null;
	}

	/**
	 * 获取单位字段
	 */
	@Override
	public String getUnitFieldName() {
		return null;
	}

	@Override
	public String getLevelFieldName() {
		return null;
	}

	@Override
	public String getDomainFieldName() {
		return null;
	}

	@Override
	public String getIsChildFieldName() {
		return null;
	}

	@Override
	public DomainType getDomainType() {
		return DomainType.Memory;
	}
}